// pages/blindMine/blindMine.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    imageUrl: app.globalData.imageUrl,
    bottabData: app.globalData.bottabDataForXq,
    imgList: [],
    formData:{},
    degreeObj: { '0': '不限', '1': '博士后', '2': '博士 ', '3': '硕士', '4': '本科', '5': '专科', '6': '高中', '7': '高中以下' },
    marriageObj: { '0': '未婚', '1': '离异', '2': '不限' },
    purchaseObj: { '0': '无', '1': '有', '2': '不限' },
    animalSignObj: { '1': '鼠', '2': '牛', '3': '虎', '4': '兔', '5': '龙', '6': '蛇', '7': '马', '8': '羊', '9': '猴', '10': '鸡', '11': '狗', '12': '猪' },
    constellationObj: { '1': '白羊座', '2': '金牛座', '3': '双子座', '4': '巨蟹座', '5': '狮子座', '6': '处女座', '7': '天秤座', '8': '天蝎座', '9': '射手座', '10': '摩羯座', '11': '水瓶座', '12': '双鱼座' },
    bloodObj: { '1': 'A型', '2': 'B型', '3': 'AB型', '4': 'O型', '5': '其他' },
    wholeMine:false,
    loadSuccess:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // if (options.emotionalDeclaration){
    //   this.setData({
    //     ['formData.basicInfo.emotionalDeclaration']: options.emotionalDeclaration
    //   })
    // }
    if (options.page =='wholeMine'){
      this.setData({
        wholeMine:true
      })
      wx.setNavigationBarTitle({
        title: '我的相亲',
      })
    }
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo')),
      ['bottabData[1].current']: true
    })
    wx.showLoading();
  },
  clickOther:function(e){

  },
  getDeatil:function(){
    var that = this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqUserBasicInfo/getMyInfo', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        userId: this.data.userInfo.id
      },
      success: function (res) {
        wx.hideLoading();
        that.setData({
          loadSuccess: true
        })
        if (res.data.success) {
          if (res.data.data.basicInfo.photos) {
            that.setData({
              imgList: res.data.data.basicInfo.photos.split(',')
            })
            res.data.data.photos = res.data.data.basicInfo.photos.split(',');
          } else {
            res.data.data.basicInfo.photo = [];
          } 
          that.setData({
            formData: res.data.data
          })
          if (res.data.data.criteria) {
            that.relaceValue('age');
            that.relaceValue('height');
            that.relaceValue('monthIncome');
          }
        }
      }
    })
  },
  relaceValue:function(value){
    if (this.data.formData.criteria[value].indexOf('-')>=0){
      var data = this.data.formData.criteria[value].split('-');
      this.setData({
        ['formData.criteria.' + value]: (data[0] == '0' ? '不限' : data[0]) + '-' + (data[1] == '0' ? '不限' : data[1])
      })
    }else{
      this.setData({
        ['formData.criteria.' + value]: '不限'
      })
    }
  },
  changeCover: function (e) {//封面照片点击事件
    var that=this;
    wx.showActionSheet({
      itemList: ['更换封面照片'],
      success: function (res) {
        if (res.tapIndex == 0) {
          wx.chooseImage({
            count: 1, // 默认9
            success: function (res) {
              that.upload(res.tempFilePaths, 'xqUserBasicInfo/uploadCoverImg', 'formData.basicInfo.coverImg', 'coverImg');
            }
          })
        }
      },
      fail: function (res) {
      }
    })
  },
  
  changePhoto: function (e) {//修改新增我的照片
    var that = this;
    var index = e.currentTarget.dataset.index;
    if (index != undefined) {
      wx.request({
        url: app.globalData.httpApiCar + 'xqUserBasicInfo/deleteMyPhoto', //仅为示例，并非真实的接口地址
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        data: {
          userId: this.data.userInfo.id,
          delPhotoPath: that.data.imgList[e.currentTarget.dataset.index]
        },
        success: function (res) {
          if (res.data.success) {
            that.data.imgList.splice(index,1);
            wx.showToast({
              title: '删除成功',
            })
            that.setData({
              imgList: that.data.imgList
            })
          }else{
            wx.showToast({
              title: '删除失败',
            })
          }
        }
      })
    }else{
      wx.chooseImage({
        count: 1, // 默认9
        // sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
        // sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
        success: function (res) {
          // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
          that.upload(res.tempFilePaths, 'xqUserBasicInfo/uploadMyPhoto', 'imgList','file');
        }
      })
    }
  },
  portraitTap:function(e){//头像点击事件
    var that=this;
    wx.showActionSheet({
      itemList: ['更换头像'],
      success: function (res) {
        if (res.tapIndex==0){
          wx.chooseImage({
            count: 1, // 默认9
            success: function (res) {
              that.upload(res.tempFilePaths, 'tGyUserInfo/uploadUrlImg', 'formData.basicInfo.imgUrl','urlImg');
            }
          })
        }
      },
      fail: function (res) {
      }
    })
  },
  editInfo:function(e){
    if (e.currentTarget.id=='1'){
      wx.setStorage({
        key: 'criteria',
        data: this.data.formData.criteria
      })
      wx.navigateTo({
        url: '/pages/blindCriteria/blindCriteria'
      })
    } else if (e.currentTarget.id == '2'){
      wx.setStorage({
        key: 'basicInfo',
        data: this.data.formData.basicInfo
      })
      wx.navigateTo({
        url: '/pages/blindBasicInfo/blindBasicInfo',
      })
    }else{
      wx.setStorage({
        key: 'xqArchives',
        data: this.data.formData.xqArchives
      })
      wx.navigateTo({
        url: '/pages/blindFile/blindFile',
      })
    }
    
  },
  /*上传图片*/
  upload: function (path,url,key,name) {
    var that = this;
    wx.showToast({
      icon: "loading",
      title: "正在上传"
    }),
      wx.uploadFile({
        url: app.globalData.httpApiCar + url,
        filePath: path[0],
        name: name,
        header: { "Content-Type": "multipart/form-data" },
        formData: {
          userId:that.data.userInfo.id
        },
        success: function (res) {
          if (res.statusCode != 200) {
            wx.showModal({
              title: '提示',
              content: '上传失败',
              showCancel: false
            })
            return;
          }
          var url = JSON.parse(res.data).data;
          if (url.url){
            url = url.url;
          }
          if (url.coverImg){
            url = url.coverImg;
          }
          that.setData({
            [key]:  url
          })
        },
        fail: function (e) {
          wx.showModal({
            title: '提示',
            content: '上传失败',
            showCancel: false
          })
        },
        complete: function () {
          wx.hideToast();  //隐藏Toast
        }
      })
  },
  switchChange: function (e) {//个人信息是否对外公开
    var that=this;
    wx.request({
      url: app.globalData.httpApiCar + 'xqUserBasicInfo/update', //仅为示例，并非真实的接口地址
      method: 'POST',
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        userId: this.data.userInfo.id,
        openId: e.detail.value?0:1
      },
      success: function (res) {
        if (res.data.success) {
          that.setData({
            'formData.basicInfo.openId': that.data.formData.basicInfo.openId==0?1:0
          })
        }
        else {
          wx.showToast({
            title: res.data.error,
            icon: 'none',
            duration: 2000
          })
        }
      }
    })
  },
  // upload
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
    // wx.showLoading();
    // this.setData({
    //   loadSuccess:false
    // })
    this.getDeatil();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})